package com.youlai.boot.modules.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.youlai.boot.modules.content.converter.LzCategoryConverter;
import com.youlai.boot.modules.content.mapper.LzCategoryMapper;
import com.youlai.boot.modules.content.model.entity.LzCategory;
import com.youlai.boot.modules.content.model.form.LzCategoryForm;
import com.youlai.boot.modules.content.model.query.LzCategoryQuery;
import com.youlai.boot.modules.content.model.vo.LzCategoryVO;
import com.youlai.boot.modules.content.service.LzCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 分类服务实现类
 *
 * @author youlaitech
 * @since 2025-02-20 11:07
 */
@Service
@RequiredArgsConstructor
public class LzCategoryServiceImpl extends ServiceImpl<LzCategoryMapper, LzCategory> implements LzCategoryService {

    private final LzCategoryConverter lzCategoryConverter;

    /**
     * 获取分类分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<LzCategoryVO>} 分类分页列表
     */
    @Override
    public IPage<LzCategoryVO> getLzCategoryPage(LzCategoryQuery queryParams) {
        Page<LzCategoryVO> pageVO = this.baseMapper.getLzCategoryPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        // 将父分类id 对应到分类名
        pageVO.getRecords().forEach(item -> {
            if (item.getPid() != null) {
                LzCategory parentCategory = this.getById(item.getPid());
                // 判断如果不为null 则设置父分类名称
                if (parentCategory != null) {
                    item.setParentName(parentCategory.getName());
                }

            }
        });
        return pageVO;
    }

    /**
     * 获取分类表单数据
     *
     * @param id 分类ID
     * @return
     */
    @Override
    public LzCategoryForm getLzCategoryFormData(Long id) {
        LzCategory entity = this.getById(id);
        return lzCategoryConverter.toForm(entity);
    }

    /**
     * 新增分类
     *
     * @param formData 分类表单对象
     * @return
     */
    @Override
    public boolean saveLzCategory(LzCategoryForm formData) {
        LzCategory entity = lzCategoryConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新分类
     *
     * @param id       分类ID
     * @param formData 分类表单对象
     * @return
     */
    @Override
    public boolean updateLzCategory(Long id, LzCategoryForm formData) {
        LzCategory entity = lzCategoryConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除分类
     *
     * @param ids 分类ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLzCategorys(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的分类数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public boolean updateLzCategoryStatus(Long id, Integer status) {
        if (status == 0 || status == 1) {
            LambdaUpdateChainWrapper<LzCategory> updateWrapper = new LambdaUpdateChainWrapper<>(this.baseMapper);
            updateWrapper.eq(LzCategory::getId, id);
            updateWrapper.set(LzCategory::getStatus, status);
            return updateWrapper.update();
        }
        return false;
    }

    @Override
    public ArrayList<LzCategoryVO> getAllLzCategorys() {
        // 获取所有的分类数据
        List<LzCategory> lzCategoryList = this.list();
        // 转换为 VO
        if (lzCategoryList != null) {
            return lzCategoryList.stream()
                    .map(lzCategory -> {
                        LzCategoryVO lzCategoryVO = new LzCategoryVO();
                        lzCategoryVO.setId(lzCategory.getId());
                        lzCategoryVO.setName(lzCategory.getName());
                        lzCategoryVO.setPid(lzCategory.getPid());
                        lzCategoryVO.setDescription(lzCategory.getDescription());
                        lzCategoryVO.setStatus(lzCategory.getStatus());
                        return lzCategoryVO;
                    }).collect(Collectors.toCollection(ArrayList::new));
        }

        return null;
    }

}
